#include "ResUpMgr.h"
#include "ZCmnFile.h"
#include <DbgHelp.h>
#include <atlconv.h>
#include <algorithm>
#include "process.h"
#include <cstdlib>
#include "GlobalDefs.h"
#pragma comment(lib,"DbgHelp")
#pragma comment(lib,"atlsd")
#include <string>
using std::wstring;

CResUpMgr::CResUpMgr(void)
{
}

CResUpMgr::~CResUpMgr(void)
{
}

void CResUpMgr::MkDir(string _FileOrDir,bool _IsFile/* = false*/)
{
	if (!_IsFile && !_FileOrDir.empty() && _FileOrDir[_FileOrDir.size()-1]!='\\') {
		_FileOrDir += "\\";
	}
	if (!PathFileExists(_FileOrDir.c_str())) {
		::MakeSureDirectoryPathExists(_FileOrDir.c_str());
	}
}

void CResUpMgr::FindAllFile(const string& _RootName,const string& _DirName,vector<string>& _FileList)
{
	WIN32_FIND_DATA _FindData;
	HANDLE hFind = INVALID_HANDLE_VALUE;
	string _FindPath = _DirName.empty()?(_RootName+"\\*.*"):(_RootName+"\\"+_DirName+"\\*.*");
	hFind = FindFirstFile(_FindPath.c_str(), &_FindData);
	if (hFind == INVALID_HANDLE_VALUE)
		return;

	string _FindFileName;
	do {
		_FindFileName = _FindData.cFileName;
		if (_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			if (_FindFileName=="." || _FindFileName=="..") {
				continue;
			}
			else {
				string _NewDir = _DirName.empty()?_FindFileName:_DirName+"\\"+_FindFileName;
				FindAllFile(_RootName,_NewDir,_FileList);
			}
		}
		else {
			string _File = _DirName.empty()?_FindFileName:_DirName+"\\"+_FindFileName;
			if (_File != "log.txt" && _File != "PckArch.txt" && _File != "Pck.txt") {
				_FileList.push_back(_File);
			}
		}
	} while (FindNextFile(hFind, &_FindData) != 0);
	FindClose(hFind);
}